package org.jeecg.eqp.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.basic.entity.MesLineCfg;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.FilePathUtil;
import org.jeecg.eqp.dto.EqpSpchkValueParamVO;
import org.jeecg.eqp.entity.EqpSpchkdetailValue;
import org.jeecg.eqp.entity.EqpSpchkheadValue;
import org.jeecg.eqp.service.IEqpSpchkdetailValueService;
import org.jeecg.eqp.service.IEqpSpchkheadValueService;
import org.jeecg.eqp.vo.EqpMaintainDetailVO;
import org.jeecg.eqp.vo.EqpSpchkdetailValueVO;
import org.jeecg.eqp.vo.EqpSpchkheadValueVO;
import org.jeecg.qis.entity.QisRuleCfg;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Description: 点检记录表
 * @Author: shaolong
 * @Date: 2022-10-13
 * @Version: V1.0
 */
@Api(tags = "点检记录表")
@RestController
@RequestMapping("/eqp/eqpSpchkheadPCValue")
@Slf4j
public class EqpSpchkheadValuePCController extends JeecgController<EqpSpchkheadValue, IEqpSpchkheadValueService> {

	@Autowired
	private IEqpSpchkheadValueService eqpSpchkheadValueService;

	@Autowired
	private IEqpSpchkdetailValueService eqpSpchkdetailValueService;
	@Lazy
	@Resource
	private CommonAPI commonApi;


	/*---------------------------------主表处理-begin-------------------------------------*/

	/**
	 * 分页列表查询  erp
	 *
	 * @param eqpSpchkheadValue
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "点检记录表-分页列表查询")
	@ApiOperation(value = "点检记录表-分页列表查询", notes = "点检记录表-分页列表查询")
	@GetMapping(value = "/list")
	@DS("#header.his")
	public Result<IPage<EqpSpchkheadValue>> queryPageList(EqpSpchkheadValue eqpSpchkheadValue,
														  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
														  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
														  HttpServletRequest req) {
		QueryWrapper<EqpSpchkheadValue> queryWrapper = QueryGenerator.initQueryWrapper(eqpSpchkheadValue, req.getParameterMap());
		Page<EqpSpchkheadValue> page = new Page<EqpSpchkheadValue>(pageNo, pageSize);
		IPage<EqpSpchkheadValue> pageList = eqpSpchkheadValueService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 分页列表查询  erp
	 *
	 * @param param
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "查询待保养、点检、维修记录列表", notes = "查询待保养记录列表")
	@GetMapping(value = "/getEqpRecordList")
	@DS("#header.his")
	public Result<IPage<EqpSpchkheadValueVO>> getEqpRecordList(EqpSpchkheadValueVO param,
															 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
															 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
															 HttpServletRequest req) {
		Page<EqpSpchkheadValueVO> page = new Page<>(pageNo, pageSize);
		IPage<EqpSpchkheadValueVO> pageList = eqpSpchkheadValueService.getEqpRecordList(page,param);
		return Result.OK(pageList);
	}

	/**
	 * 分页列表查询
	 *
	 * @param eqpSpchkheadValue
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "获取详细信息", notes = "获取详细信息")
	@GetMapping(value = "/queryEqpDetailList")
	@DS("#header.his")
	public Result<List<EqpMaintainDetailVO>> queryEqpDetailList(EqpSpchkheadValue eqpSpchkheadValue,
																HttpServletRequest req) {
		List<EqpMaintainDetailVO> pageList = eqpSpchkheadValueService.queryEqpDetailList(eqpSpchkheadValue);
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 *
	 * @param eqpSpchkheadValue
	 * @return
	 */
	@AutoLog(value = "点检记录表-添加")
	@ApiOperation(value = "点检记录表-添加", notes = "点检记录表-添加")
	@PostMapping(value = "/add")
	@DS("#header.line")
	public Result<String> add(@RequestBody EqpSpchkheadValue eqpSpchkheadValue) {
		Integer counterValue = commonApi.getCounterValue("recid.eqp_spchkhead_value","0");
		eqpSpchkheadValue.setRecId(counterValue);
		eqpSpchkheadValue.setStime(new Date());
		eqpSpchkheadValue.setStatus("0");
		//spchkno
		String spchkno = generateDateSerial("MT", counterValue);
		eqpSpchkheadValue.setSpchkno(spchkno);
		eqpSpchkheadValueService.save(eqpSpchkheadValue);
		return Result.OK("添加成功！");
	}

	public static String generateDateSerial(String prefix, int sequence) {
		String formattedNumber = String.format("%04d", sequence);
		LocalDate currentDate = LocalDate.now();
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
		String formattedDate = currentDate.format(formatter);
//		String formattedNumber = String.format("%0" + sequenceLength + "d", getNextSequence());
		return prefix + formattedDate + formattedNumber;
	}

	/**
	 * 编辑
	 *
	 * @param eqpSpchkheadValue
	 * @return
	 */
	@AutoLog(value = "点检记录表-编辑")
	@ApiOperation(value = "点检记录表-编辑", notes = "点检记录表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
	@DS("#header.line")
	public Result<String> edit(@RequestBody EqpSpchkheadValue eqpSpchkheadValue) {
		eqpSpchkheadValueService.edit(eqpSpchkheadValue);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param eqpSpchkheadValue
	 * @return
	 */
	@AutoLog(value = "点检记录表-通过id删除")
	@ApiOperation(value = "点检记录表-通过id删除", notes = "点检记录表-通过id删除")
	@RequestMapping(value = "/delete")
	@DS("#header.line")
	public Result<String> delete(@RequestBody EqpSpchkheadValue eqpSpchkheadValue) {
		eqpSpchkheadValueService.removeById(eqpSpchkheadValue);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "点检记录表-批量删除")
	@ApiOperation(value = "点检记录表-批量删除", notes = "点检记录表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@DS("#header.line")
	public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.eqpSpchkheadValueService.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 导出
	 *
	 * @return
	 */
	@RequestMapping(value = "/exportXls")
	@DS("#header.his")
	public ModelAndView exportXls(HttpServletRequest request, EqpSpchkheadValue eqpSpchkheadValue) {

		return super.exportXls(request, eqpSpchkheadValue, EqpSpchkheadValue.class, "点检记录表");
	}

	@RequestMapping("/exportReport")
	@DS("#header.his")
	public void exportReport(@RequestBody List<EqpSpchkheadValue> headList, HttpServletResponse response) {
		eqpSpchkheadValueService.exportExcel(headList, response);
	}

	/**
	 * 导入
	 *
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@DS("#header.line")
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, EqpSpchkheadValue.class);
	}
	/*---------------------------------主表处理-end-------------------------------------*/


	/*--------------------------------子表处理-点检记录详细表-begin----------------------------------------------*/

	/**
	 * 通过主表ID查询1
	 *
	 * @return
	 */
	//@AutoLog(value = "点检记录详细表-通过主表ID查询")
	@ApiOperation(value = "点检记录详细表-通过主表ID查询", notes = "点检记录详细表-通过主表ID查询")
	@GetMapping(value = "/listEqpSpchkdetailValueByMainId")
	@DS("#header.his")
	public Result<IPage<EqpSpchkdetailValueVO>> listEqpSpchkdetailValueByMainId(EqpSpchkdetailValue param,
																			  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
																			  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
																			  HttpServletRequest req) {
		Page<EqpSpchkdetailValueVO> page = new Page<>(pageNo, pageSize);
		IPage<EqpSpchkdetailValueVO> pageList = eqpSpchkdetailValueService.pageDetailList(page, param);
		return Result.OK(pageList);
	}



	/**
	 * 添加
	 *
	 * @param eqpSpchkdetailValue
	 * @return
	 */
	@AutoLog(value = "点检记录详细表-添加")
	@ApiOperation(value = "点检记录详细表-添加", notes = "点检记录详细表-添加")
	@PostMapping(value = "/addEqpSpchkdetailValue")
	@DS("#header.line")
	public Result<String> addEqpSpchkdetailValue(@RequestBody EqpSpchkdetailValue eqpSpchkdetailValue) {
		Integer counterValue = commonApi.getCounterValue("recid.eqp_spchkdetail_value","0");
		eqpSpchkdetailValue.setRecId(counterValue);
		eqpSpchkdetailValueService.save(eqpSpchkdetailValue);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param eqpSpchkdetailValue
	 * @return
	 */
	@AutoLog(value = "点检记录详细表-编辑")
	@ApiOperation(value = "点检记录详细表-编辑", notes = "点检记录详细表-编辑")
	@RequestMapping(value = "/editEqpSpchkdetailValue", method = {RequestMethod.PUT, RequestMethod.POST})
	@DS("#header.line")
	public Result<String> editEqpSpchkdetailValue(@RequestBody EqpSpchkdetailValue eqpSpchkdetailValue) {
		Result<String> res = eqpSpchkdetailValueService.edit(eqpSpchkdetailValue);

		return Result.OK("编辑成功!");
	}

	/**
	 * 批量修改点检记录详情
	 * @param paramVO
	 * @return
	 */
	@AutoLog(value = "批量修改点检记录详情")
	@ApiOperation(value = "批量修改点检记录详情", notes = "批量修改点检记录详情")
	@RequestMapping(value = "/editBatchEqpSpchkdetailValue", method = {RequestMethod.PUT, RequestMethod.POST})
	@DS("#header.line")
	public Result<String> editBatchEqpSpchkdetailValue(@RequestBody EqpSpchkValueParamVO paramVO) {
		EqpSpchkheadValue head = paramVO.getHead();
		head.setStatus("1");//将状态置为已处理
		head.setCtime(new Date());
		eqpSpchkheadValueService.updateById(head);
		paramVO.getDetailList().forEach(item -> {
			if (item.getRecId() == null) {
				Integer counterValue = commonApi.getCounterValue("recid.eqp_spchkdetail_value","0");
				item.setRecId(counterValue);
				eqpSpchkdetailValueService.save(item);
			} else {
				item.setCtime(new Date());
				eqpSpchkdetailValueService.updateById(item);
			}

		});
//		if (list == null || list.isEmpty()) {
//			return Result.error("参数不能为空");
//		}
//		list.forEach(item -> {
//			eqpSpchkdetailValueService.updateById(item);
//		});
		return Result.OK("批量修改成功!");
	}

	/**
	 * 删除
	 *
	 * @param eqpSpchkdetailValue
	 * @return
	 */
	@AutoLog(value = "点检记录详细表-通过id删除")
	@ApiOperation(value = "点检记录详细表-通过id删除", notes = "点检记录详细表-通过id删除")
	@RequestMapping(value = "/deleteEqpSpchkdetailValue")
	@DS("#header.line")
	public Result<String> deleteEqpSpchkdetailValue(@RequestBody EqpSpchkdetailValue eqpSpchkdetailValue) {
		boolean res = eqpSpchkdetailValueService.removeById(eqpSpchkdetailValue);
		if (res && StringUtils.isNotBlank(eqpSpchkdetailValue.getImage())) {
			FilePathUtil util = new FilePathUtil();
			boolean flag = util.deleteFile(eqpSpchkdetailValue.getImage());
			if (!flag) {
				throw new RuntimeException("记录删除失败");
			}
		}
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param list
	 * @return
	 */
	@AutoLog(value = "点检记录详细表-批量删除")
	@ApiOperation(value = "点检记录详细表-批量删除", notes = "点检记录详细表-批量删除")
	@RequestMapping(value = "/deleteBatchEqpSpchkdetailValue")
	@DS("#header.line")
	public Result<String> deleteBatchEqpSpchkdetailValue(@RequestBody List<EqpSpchkdetailValue> list) {
		list.forEach(item -> {
			boolean res = eqpSpchkdetailValueService.removeById(item);
			if (res && StringUtils.isNotBlank(item.getImage())) {
				FilePathUtil util = new FilePathUtil();
				boolean flag = util.deleteFile(item.getImage());
				if (!flag) {
					throw new RuntimeException("记录删除失败");
				}
			}
		});
		return Result.OK("批量删除成功!");
	}

	/**
	 * 导出
	 *
	 * @return
	 */
	@RequestMapping(value = "/exportEqpSpchkdetailValue")
	@DS("#header.his")
	public ModelAndView exportEqpSpchkdetailValue(HttpServletRequest request, EqpSpchkdetailValue eqpSpchkdetailValue) {
		// Step.1 组装查询条件
		QueryWrapper<EqpSpchkdetailValue> queryWrapper = QueryGenerator.initQueryWrapper(eqpSpchkdetailValue, request.getParameterMap());
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		// Step.2 获取导出数据
		List<EqpSpchkdetailValue> pageList = eqpSpchkdetailValueService.list(queryWrapper);
		List<EqpSpchkdetailValue> exportList = null;

		// 过滤选中数据2
//		 String selections = request.getParameter("selections");
//		 if (oConvertUtils.isNotEmpty(selections)) {
//			 List<String> selectionList = Arrays.asList(selections.split(","));
//			 exportList = pageList.stream().filter(item -> selectionList.contains(item.getHeadid())).collect(Collectors.toList());
//		 } else {
//			 exportList = pageList;
//		 }

		// Step.3 AutoPoi 导出Excel
		ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		//此处设置的filename无效,前端会重更新设置一下
		mv.addObject(NormalExcelConstants.FILE_NAME, "点检记录详细表");
		mv.addObject(NormalExcelConstants.CLASS, EqpSpchkdetailValue.class);
		mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("点检记录详细表报表", "导出人:" + sysUser.getRealname(), "点检记录详细表"));
		mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
		return mv;
	}



}
